package com.sky.listener;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.sky.service.WmsWareOrderTaskService;
import com.sky.service.WmsWareSkuService;
import com.sky.to.rabbitmq.StockLockTo;
import jakarta.annotation.Resource;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 * 监听锁库存服务
 */
@RabbitListener(queues = "stock.release.queue")
@Service
public class StockListener {

    @Resource
    private WmsWareOrderTaskService wareOrderTaskService;

    @RabbitHandler
    public void handleStockRelease(Channel channel, Message message) throws Exception{
        String s = new String(message.getBody());
        StockLockTo stockLockTo = JSON.parseObject(s, StockLockTo.class);
        boolean stockRelease = wareOrderTaskService.handleStockRelease(stockLockTo);
        if (stockRelease){
            // 解锁成功，手动消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } else {
            // 解锁失败，消息重新放回队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
